<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="商品名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                    <el-button type="primary">查询</el-button>
                    <el-button type="warning" @click="$router.push('/product/add')">新增</el-button>
                    </el-button-group>
                </el-form-item>
                <el-form-item>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="products" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" width="60" label="ID" sortable></el-table-column>
            <el-table-column prop="product_name" label="名称" sortable></el-table-column>
            <el-table-column property="path" label="图片" align="center">
                <template slot-scope="scope">
                    <el-image
                        style="width: 50px; height: auto;"
                        :src="scope.row.product_image"
                        :preview-src-list="[scope.row.product_image]">
                        >
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="price" label="原始价格"></el-table-column>
            <el-table-column prop="discount_price" label="优惠价格"></el-table-column>
            <el-table-column prop="coin" label="币种"></el-table-column>
            <el-table-column prop="price_rmb" label="人民币价格"></el-table-column>
            <el-table-column prop="description" label="描述"></el-table-column>
            <el-table-column prop="created_at" label="创建时间"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="per_page" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
                <el-form-item label="产品名称" prop="product_name" required>
                    <el-input placeholder="请输入产品名称" v-model="editForm.product_name"></el-input>
                </el-form-item>
                <el-form-item label="产品图片" prop="product_image" required>
                    <el-input type="hidden" v-model="editForm.product_image"></el-input>
                    <el-upload
                        name="file"
                        :limit="1"
                        class="upload-demo"
                        action="admin/upload/image_upload"
                        :auto-upload="true"
                        :multiple="false"
                        :data="{'type': 'product'}"
                        :on-remove="handleRemove"
                        :on-success="uploadSuccess"
                        :on-preview="handlePictureCardPreview"
                        :file-list="fileList"
                        list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-form-item prop="category_id" label="所属分类">
                    <el-select v-model="editForm.category_id" placeholder="请选择所属分类">
                        <el-option v-for="(item,index) in categories" :key="index" :label="item.text" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="command_type" label="推荐类型">
                    <el-select v-model="editForm.command_type" placeholder="请选择推荐类型">
                        <el-option label="普通" value="normal"></el-option>
                        <el-option label="热门推荐" value="hot"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="price" label="商品售价">
                    <el-input placeholder="请填写商品售价" v-model="editForm.price"></el-input>
                </el-form-item>
                <el-form-item prop="discount_price" label="优惠价格">
                    <el-input placeholder="请填写优惠价格" v-model="editForm.discount_price"></el-input>
                </el-form-item>
                <el-form-item prop="coin" label="币种">
                    <el-select v-model="editForm.coin" placeholder="请选择商品币种">
                        <el-option label="美元" value="USD"></el-option>
                        <el-option label="英镑" value="GBP"></el-option>
                        <el-option label="欧元" value="EUR"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="price_rmb" label="价格（RMB）">
                    <el-input placeholder="请填写人民币价格" v-model="editForm.price_rmb"></el-input>
                </el-form-item>
                <el-form-item prop="description" label="商品详情">
                    <el-input placeholder="请填写商品详情" v-model="editForm.description"></el-input>
                </el-form-item>
                <el-form-item prop="contact_wx" label="联系人微信">
                    <el-input placeholder="请填写联系人微信" v-model="editForm.contact_wx"></el-input>
                </el-form-item>
                <el-form-item prop="sale_nums" label="已售数量">
                    <el-input placeholder="请填写已售数量" v-model="editForm.sale_nums"></el-input>
                </el-form-item>
                <el-form-item label="购买流程">
                    <quill-editor ref="text" v-model="editForm.purchase_notice" @change="textChange" class="myQuillEditor" :options="editorOption" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'
    import { quillEditor } from 'vue-quill-editor'
    import {getProduct, removeProduct, getCategory, removeFile, editProduct} from '../../api/api';

    export default {
        components: {
            quillEditor
        },
        data() {
            return {
                filters:{
                    name: ''
                },
                categories: [
                    {id: '', text: ''}
                ],
                editorOption: {
                    theme: 'snow'
                },
                c_image:{},
                dialogImageUrl: '',
                dialogVisible: false,
                disabled: false,
                products: [],
                fileList: [],
                total: 0,
                per_page: 0,
                page: 1,
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {

                }
            }
        },
        methods: {
            textChange () {
                console.log(this.$refs.text.value)
            },
            handleRemove(file) {
                var file_name;
                if(file.response !== undefined){
                    file_name = file.response.data;
                }else{
                    file_name = file.url;
                }
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    if(res.code == 200){
                        this.editForm.image_url = ''
                    }
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.get_product();
            },
            uploadSuccess(res){
                this.editForm.product_image = res.data
            },
            getCategory(){
                getCategory().then((res) => {
                    this.categories = res.data.data;
                })
            },
            fileRemove(file) {
                console.log(file);
                var file_name = file.response.data;
                var data = {'file_name': file_name};
                removeFile(data).then((res) => {
                    console.log(res);
                    if(res.code == 200){
                        this.form.product_image = ''
                    }
                })
            },
            //获取商品列表
            get_product() {
                let para = {
                    page: this.page,
                };
                var this_ = this;
                this_.listLoading = true;
                getProduct(para).then((res) => {
                    this_.total = res.data.data.total;
                    this_.per_page = res.data.data.per_page;
                    this_.products = res.data.data.data;
                    this_.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { product_id: row.id };
                    removeProduct(para).then((res) => {
                        this.listLoading = false;
                        console.log(res);
                        if (res.code == 200){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.get_product();
                        }else {
                            this.$message({
                                message: res.msg,
                                type: 'fail'
                            });
                        }

                    });
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.getCategory();
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
                this.fileList = [{'name': row.product_name, 'url': row.product_image}]
                console.log(this.fileList)
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            editProduct(para).then((res) => {
                                if (res.success){
                                    this.$message({
                                        message: res.msg,
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.get_product();
                                }else {
                                    this.$message({
                                        message: res.msg,
                                        type: 'error'
                                    });
                                }
                                this.editLoading = false;

                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { ids: ids };
                    batchRemoveUser(para).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.get_product();
                    });
                });
            }
        },
        mounted() {
            this.get_product();
        }
    }

</script>

<style scoped>
    .originImg{
        width: 100%;
    }
    .el-button{
        margin-left: 0 !important;
    }
</style>
